package com.example.weixinpayment.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * @program: weixin-payment
 * @description: mybatisPlus配置类 启用事务
 * @author: LiuZhuzheng
 * @create: 2021-12-28 19:33
 **/
@Slf4j
@Configuration
@MapperScan("com.example.weixinpayment.mapper")
@EnableTransactionManagement
public class MybatisPlusConfig {

    @Bean
    public MetaObjectHandler myMetaObjectHandle(){
        return new MetaObjectHandler() {
            @Override
            public void insertFill(MetaObject metaObject) {
                log.info("start insert fill ...");
                this.strictInsertFill(metaObject, "createTime", () -> Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant()), Date.class);
            }

            @Override
            public void updateFill(MetaObject metaObject) {
                log.info("start update fill ...");
                this.strictUpdateFill(metaObject, "updateTime", () -> Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant()), Date.class);
            }
        };
    }
}
